package com.atguigu.hellomp.minioTest;

import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.UploadObjectArgs;
import io.minio.errors.*;
import org.joda.time.DateTime;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @BelongsProject: lease
 * @BelongsPackage: com.atguigu.hellomp.minioTest
 * @Author: jiangyi
 * @CreateTime: 2025-08-15  11:28
 * @Description: TODO
 * @Version: 1.0
 */
public class testUpload {

    public static void main(String[] args) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 创建MinioClient对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.10.100:9000")
                .credentials("minioadmin", "minioadmin")
                .build();
        // 存储桶名称
        String bucketName = "lease-0528";

        // 创建上传文件参数--文件夹格式
        String date = new DateTime().toString("yyyy/MM/dd/");
        String fileName = "rabbit.jpg" ;
        String filenameExtension = StringUtils.getFilenameExtension(fileName);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String uuidExtension  =  uuid + "." + filenameExtension;


        UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                .filename("C:/Users/17779/OneDrive/Pictures/rabbit.jpg") // 本地文件路径
                .bucket(bucketName) // 存储桶名称
                .object(date + uuidExtension)  // 存储至minio中的文件名
                .build();
        // 上传文件
        ObjectWriteResponse objectWriteResponse = minioClient.uploadObject(uploadObjectArgs);
    }


}
